/* eslint no-console:0 */
//
// Demonstrate some of the basics.

// import { get } from 'https';

//
const Client = require('kubernetes-client').Client;
const client = new Client({ version: '1.9' });


function get_ips() {
    /* Get all the ns3 service. */
    try {
        client.api.v1.namespaces('default').pods().get().then(service => {
            items = service.body.items;
            console.log('items: ', items);
            ns3_items = items.filter(item => item.metadata.labels.app == 'node-ns3' && item.status.phase == 'Running');
            ips = ns3_items.map(item => item.status.podIP);
            console.log('ips: ', ips);
            return ips;
        })
    } catch (err) {
        console.error('Error: ', err);
    }
}

function set_ns3_num(num) {
    /*  Change the ns3 Deployment Replica count */
    try {

        const replica = {
            spec: {
                replicas: num
            }
        };

        client.apis.apps.v1.namespaces('default').deployments('node-ns3').patch({ body: replica }).then(replicaModify => {
            console.log('Replica Modification: ', replicaModify);
        });

    } catch (err) {
        console.error('Error: ', err);
    }
}

// get_ips()
// set_ns3_num(2)

module.exports = {
    set_ns3_num,
    get_ips
}